package frame;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import bean.Driver;
import dao.DriverDao;
import dao.DriverDaoImpl;

import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class DriverTableFrame {

    private JFrame frame;
    private JTable table;

    DriverDao drdao = new DriverDaoImpl();
    private JTextField textField;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DriverTableFrame window = new DriverTableFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public DriverTableFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 695, 562);
        frame.setLocationRelativeTo(null); // Ĭ Ͼ     ʾ
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        //      м            Զ
        MYPanel bg = new MYPanel();
        bg.setBounds(0, 0, 677, 515);
        //           ӵ frame  Ĭ ϴ
        frame.getContentPane().add(bg);
        //     bg Ĳ   Ϊ   Բ
        bg.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(14, 52, 649, 332);
        bg.add(panel);
        panel.setLayout(null);

        table = new JTable();
        tableInit(table);
        table.setBounds(0, 0, 649, 390);


        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(0, 0, 649, 332);
        panel.add(scrollPane);
        //  ʾ
        TableModel dataModel =getDataModel();
        table.setModel(dataModel);

        JButton button = new JButton("\u6DFB\u52A0\u4FE1\u606F");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DriverInformationFrame dif = new DriverInformationFrame();
            }
        });
        button.setBounds(31, 475, 113, 27);
        bg.add(button);

        JButton button_1 = new JButton("\u4FEE\u6539\u4FE1\u606F");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DriverInformationFrame dif = new DriverInformationFrame();
            }
        });
        button_1.setBounds(185, 475, 113, 27);
        bg.add(button_1);

        JButton button_2 = new JButton("\u5220\u9664\u4FE1\u606F");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DriverInformationFrame dif = new DriverInformationFrame();
            }
        });
        button_2.setBounds(329, 475, 113, 27);
        bg.add(button_2);

        JButton button_3 = new JButton("\u5237\u65B0\u4FE1\u606F");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TableModel changeddataMode1 = getDataModel();
                table.setModel(changeddataMode1);
            }
        });
        button_3.setBounds(477, 475, 113, 27);
        bg.add(button_3);

        textField = new JTextField();
        textField.setBounds(58, 422, 86, 24);
        bg.add(textField);
        textField.setColumns(10);

        JButton button_4 = new JButton("\u67E5\u8BE2\u4FE1\u606F");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DriverDaoImpl dridao=new DriverDaoImpl();
                Driver driver=new Driver();
                driver.setDid(Integer.parseInt(textField.getText()));
                //         ֶ         ͷ
                String[] columnNames = drdao.getColumnNames("driverinfortable");
                //   ò ѯ     list б  ת  Ϊ  ά   飬
                List<Driver> driverList = drdao.getDriverBydid(Integer.parseInt(textField.getText()));
                int row = driverList.size(); //               ݸ
                int column = columnNames.length; //             ֶθ

                Object[][] data = new Object[row][column];
                for (int i = 0; i < driverList.size(); i++) {
                    data[i][0] = driverList.get(i).getDid();
                    data[i][1] = driverList.get(i).getDname();
                    data[i][2] = driverList.get(i).getDsex();
                    data[i][3] = driverList.get(i).getDbirth();
                    data[i][4] = driverList.get(i).getDaddress();
                    data[i][5] = driverList.get(i).getDtype();
                    data[i][6] = driverList.get(i).getDnum();
                    data[i][7] = driverList.get(i).getDusetime();
                }

                TableModel dataModel = getDataModelselect();//            ģ
                table.setModel(dataModel);
            }
        });
        button_4.setBounds(504, 421, 113, 27);
        bg.add(button_4);


    }
    protected TableModel getDataModel() {

        //         ֶ         ͷ
        String[] columnNames = drdao.getColumnNames("driverinfortable");

        //   ò ѯ     list б  ת  Ϊ  ά   飬
        List<Driver> driverList = drdao.getDriverbySth();
        int row = driverList.size(); //               ݸ
        int column = columnNames.length; //             ֶθ

        Object[][] data = new Object[row][column];
        for (int i = 0; i < driverList.size(); i++) {
            data[i][0] = driverList.get(i).getDid();
            data[i][1] = driverList.get(i).getDname();
            data[i][2] = driverList.get(i).getDsex();
            data[i][3] = driverList.get(i).getDbirth();
            data[i][4] = driverList.get(i).getDaddress();
            data[i][5] = driverList.get(i).getDtype();
            data[i][6] = driverList.get(i).getDnum();
            data[i][7] = driverList.get(i).getDusetime();
        }

        TableModel dataModel = new DefaultTableModel(data, columnNames); //            ģ

        return dataModel;
    }
    protected TableModel getDataModelselect() {

        //         ֶ         ͷ
        String[] columnNames = drdao.getColumnNames("driverinfortable");

        //   ò ѯ     list б  ת  Ϊ  ά   飬
        List<Driver> driverList = drdao.getDriverBydid(Integer.parseInt(textField.getText()));
        int row = driverList.size(); //               ݸ
        int column = columnNames.length; //             ֶθ

        Object[][] data = new Object[row][column];
        for (int i = 0; i < driverList.size(); i++) {
            data[i][0] = driverList.get(i).getDid();
            data[i][1] = driverList.get(i).getDname();
            data[i][2] = driverList.get(i).getDsex();
            data[i][3] = driverList.get(i).getDbirth();
            data[i][4] = driverList.get(i).getDaddress();
            data[i][5] = driverList.get(i).getDtype();
            data[i][6] = driverList.get(i).getDnum();
            data[i][7] = driverList.get(i).getDusetime();
        }

        TableModel dataModel = new DefaultTableModel(data, columnNames); //            ģ

        return dataModel;
    }
    private void tableInit(JTable tb) {

        //  Ԫ
        DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
        cr.setHorizontalAlignment(JLabel.CENTER);
        tb.setDefaultRenderer(Object.class, cr);

        //     и
        tb.setRowHeight(60);

        //         С
        tb.setFont(new Font("΢   ź ", 0, 20));
        tb.getTableHeader().setFont(new Font("΢   ź ", 0, 20));



    }
}
//    ͼ
class MYPanel1 extends JPanel {
    private Image img;

    // ʵ ֹ  췽
    public MYPanel1() {
        // TODO Auto-generated constructor stub
        //  ڹ  췽   ж   Ҫ  ͼƬ  Դ   г ʼ
        ImageIcon icon = new ImageIcon(UsersLoginFrame.class.getResource("/images/  ¼ҳ 汳  ͼƬ.jpg"));
        //   icon    ת    img   ͻ
        img = icon.getImage();
    }

    // JPanel     ṩ  һ        ʵ ֡           Ĺ
    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }

}